package cn.dt.ymcc.mq.consumer;

import cn.dt.ymcc.domain.CourseOrder;
import cn.dt.ymcc.dto.PayNotifyDto;
import cn.dt.ymcc.service.ICourseOrderService;
import cn.dt.ymcc.util.AssertUtil;
import com.alibaba.fastjson.JSONObject;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import java.nio.charset.StandardCharsets;

@RocketMQMessageListener(
        consumerGroup = "order-order-consumer",
        topic = "pay-notify-topic",
        selectorExpression = "pay-notify-tag",
        messageModel = MessageModel.BROADCASTING
)
public class OrderPayConsumer implements RocketMQListener<MessageExt> {
    @Autowired
    private ICourseOrderService courseOrderService;
    /**
     *
     * 1.拿到以后先根据订单号查询t_pay_order中是否存在，如果存在则放置，说明已经消费
     * 2.拿不到则保存数据
     * @param messageExt
     */
    @Override
    public void onMessage(MessageExt messageExt) {
        byte[] body = messageExt.getBody();
        AssertUtil.isNotNull(body,"消息没有拿到");
        String payOrderStr = new String(body, StandardCharsets.UTF_8);
        PayNotifyDto dto = JSONObject.parseObject(payOrderStr, PayNotifyDto.class);
        String orderNo = dto.getOrderNo();
        CourseOrder order = courseOrderService.selectByOrderNo(orderNo);
        AssertUtil.isNotNull(order,"无订单存在");

        boolean statusJudgment = order.getStatusOrder() == 1;
        if(statusJudgment){
            order.setStatusOrder(CourseOrder.STATUS_ORDER_SUCCSESS);
            courseOrderService.updateById(order);
        }





    }
}
